import axios from 'axios';
import type {
  AxiosInstance,
  AxiosRequestConfig,
  InternalAxiosRequestConfig
} from 'axios';
import type { IRequestInterceptors, Iconfig } from './type';
import { ElLoading } from 'element-plus';

const DEFAULT_LOADING = true;
//请求拦截，后写的先调用；响应拦截，先写的先调用
class Request {
  instance: AxiosInstance;
  interceptors?: IRequestInterceptors;
  showLoading: boolean;
  loading?: any;

  constructor(config: Iconfig) {
    //创建实例
    this.instance = axios.create(config);
    this.interceptors = config.interceptors;
    //loading效果，默认为true
    this.showLoading = config.showLoading ?? DEFAULT_LOADING;
    //每个实例的拦截器
    this.instance.interceptors.request.use(
      this.interceptors?.requestInterceptors,
      this.interceptors?.requestInterceptorsCatch
    );
    this.instance.interceptors.response.use(
      this.interceptors?.responseInterceptors,
      this.interceptors?.responseInterceptorsCatch
    );

    //所有实例都有的拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // this.loading的类型不明确
        // console.log(this.showLoading);
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '全力加载中...',
            background: 'rgba(0,0,0,0.7)'
          });
        }
        return config;
      },
      (err) => {
        return err;
      }
    );

    this.instance.interceptors.response.use(
      (res) => {
        // console.log(this.showLoading);
        this.loading?.close();

        const status = res.request.status;
        switch (status) {
          case 200:
            return res.data;
            break;
          case 404:
            // console.log('服务器找不到');
            break;
        }
      },
      (err) => {
        this.loading?.close();
        return err;
      }
    );
  }
  //requset方法
  request<T>(config: Iconfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      //单独请求的请求拦截器
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config);
      }
      //判断是否需要显示loading
      if (config.showLoading === false) {
        this.showLoading = config.showLoading;
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          //单独请求的响应拦截器
          if (config.interceptors?.responseInterceptors) {
            res = config.interceptors.responseInterceptors(res);
          }
          this.showLoading = DEFAULT_LOADING;
          resolve(res);
          return res;
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING;
          reject(err);
          return err;
        });
    });
  }

  get<T>(config: Iconfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' });
  }

  post<T>(config: Iconfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' });
  }

  delete<T>(config: Iconfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' });
  }

  patch<T>(config: Iconfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' });
  }
}

export default Request;
